package com.spbdx.studyspringboot.duty_chain.temp;

import com.spbdx.studyspringboot.duty_chain.chain.IHandler;
import com.spbdx.studyspringboot.duty_chain.entity.Product;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @Author: fan
 * @CreateTime 2025/1/1 21:53
 * @Description
 * @Version 1.0
 */
//@Component
public class ProductChainSpringFactory<T,R> {

    private IHandler<T,R> first;

    public ProductChainSpringFactory(List<IHandler<T,R>> handlers){

        Assert.notEmpty(handlers,"责任链集合不能为空");

        for (int i = 0; i < handlers.size()-1; i++) {
            handlers.get(i).setNext(handlers.get(i+1));
        }

        first = handlers.get(0);

    }

    /**
     * 执行具体业务场景中的责任链集合
     * @param t
     * @return
     */
//    public Boolean executeHandle(Product product){
//        return first.handle(product);
//    }

        public R executeHandle(T t){
           return first.handle(t);
    }

}
